//
//  ClockDaysVC.m
//  test
//
//  Created by 朱圳 on 2018/3/8.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "ClockDaysVC.h"
#import "ClockDaysCell.h"
#import "ClockVC.h"
@interface ClockDaysVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *daysArray;
@end

@implementation ClockDaysVC

static NSString * const reuseIdentifier = @"Cell";


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setData];
   
    [self.view addSubview:self.collectionView];
    self.title = @"打卡记录";
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     [self requestData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)setData{
    for (int i = 1; i<366; i++) {
        [self.daysArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });
}

-(void)requestData{

    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{
                           @"sid" : Sid,
                           @"spass" : Spassword,
                           };
    [[AFNetworkTool shareManager]postJSONWithUrl:Stu_Clockdays parmas:dict successData:^(id json) {
        [self.dataArray removeAllObjects];
        NSArray *arr = json[@"days"];
        for (int i = 0; i<arr.count; i++) {
            [weakSelf.dataArray addObject:arr[i][@"orderline"]];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.collectionView reloadData];
        });
    } failure:^(NSError *error) {
        [Utility showFail:@"网络连接出错..."];
        NSLog(@"error%@",error);
    }];
}


#pragma mark  ---UICollectionView---
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.daysArray.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * stringID = [NSString stringWithFormat:@"CookingEquipmentCellId%ld",indexPath.row];
    ClockDaysCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:stringID forIndexPath:indexPath];
    cell.days.textAlignment = NSTextAlignmentCenter;
    cell.days.text = self.daysArray[indexPath.row];
    
    /**
     获取当前天数 ，与cell中day相同的加 红色 _ 标示
     */
    NSString *days = [NSString stringWithFormat:@"%@",Days];
    if ([cell.days.text isEqualToString:days]) {
        
        UIView *xl = [[UIView alloc]init];
        xl.backgroundColor = [UIColor redColor];
        [cell addSubview:xl];
        [xl mas_makeConstraints:^(MASConstraintMaker *make) {
            [make.top.mas_equalTo(cell.days.mas_bottom) setOffset:-8];
            [make.left.mas_equalTo(cell.days.mas_left) setOffset:15];
            [make.right.mas_equalTo(cell.days.mas_right) setOffset:-15];
            make.height.mas_equalTo(3);
        }];
    }
    
    // 网络获取已经打卡的天数list 跟cell365天 循环对比。如果有 则设置已经打卡背景颜色标示 红色
    for (int i = 0; i<self.dataArray.count; i++) {
        if ([self.daysArray[indexPath.row] isEqualToString:self.dataArray[i]]) {
            [cell.days setBackgroundColor:HWColor(255,161,136)];
            [cell.days setTextColor:[UIColor whiteColor]];
        }
    }
    cell.days.layer.cornerRadius = 12;
    cell.days.clipsToBounds = YES;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ClockVC *vc = [[ClockVC alloc]init];
    vc.days = self.daysArray[indexPath.row];
    vc.types = @"1";
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((ScreenWidth-26)/7, 40);
}
//边距设置:整体边距的优先级，始终高于内部边距的优先级
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
    //分别为上、左、下、右
}

#pragma mark  ---懒加载---


//dataArray 为已经打开天数 数据
- (NSMutableArray *)dataArray {
    
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)daysArray {
    
    if(!_daysArray){
        _daysArray = [NSMutableArray array];
    }
    return _daysArray;
}
-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:layout];
        layout.minimumLineSpacing = 1;//设置最小行间距
        layout.minimumInteritemSpacing = 1;//item间距(最小值)
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        for (NSInteger i = 0; i < self.daysArray.count ; i++) {
            
            NSString * stringID = [NSString stringWithFormat:@"CookingEquipmentCellId%ld",i];
            [_collectionView registerNib:[UINib nibWithNibName:@"ClockDaysCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:stringID];
        }

    }
    return _collectionView;
}

@end

